package com.chaucer.demo.controller;

import com.chaucer.demo.entity.HelloWorldEntity;
import com.chaucer.demo.mapper.HelloWorldMapper;
import com.chaucer.demo.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * @Description TODO
 * @Author Chaucer
 * @Date 2021/9/3 22:55
 */
@RestController
public class HelloWorldController {
    @Autowired
    HelloService service;
    @RequestMapping(value = "/save", method = POST)
    public String insert(@RequestBody HelloWorldEntity entity) {
//        /**
//         * 为了演示方便，先删除数据
//         */
//        mapper.delete(mapper.query()
//                .where.id().eq(1L).end());
        /**
         * 插入数据
         */
//        HelloWorldEntity entity = new HelloWorldEntity();
//        entity.setId(1L);
//        entity.setSayHello("hello local");
//        entity.setYourName("fluent mybatis");
//        entity.setIsDeleted(false);
        try {
            service.save(entity);
        } catch (Exception e) {
            return "failed";
        }
        return "success";
    }
    @RequestMapping(value = "/find", method = POST)
    public HelloWorldEntity findById(@RequestParam String id){
        HelloWorldEntity entity = new HelloWorldEntity();
        try {
            entity =  service.findById(Long.parseLong(id));
        } catch (Exception e) {
            System.out.println(e);
        }
        return entity;
    }

    public static void main(String[] args) {
        System.out.println(Long.parseLong("2"));
    }
}